#include "commonpage.h"
#include "ui_commonpage.h"
#include<listitembox.h>
#include<QDebug>

CommonPage::CommonPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->playAllBtn,&QPushButton::clicked,this,[=](){
        emit playAll(musicListType);
    });
    connect(ui->pageMusicList,&QListWidget::doubleClicked,this,[=](const QModelIndex&index){

        qDebug()<<"触发了双击槽函数，行数为："<<index.row();
        emit playMusicByIndex(this,index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setMusicListType(MusicListType type)
{
    this->musicListType=type;
}

void CommonPage::setCommonUi(const QString &text, const QString &imagePath)
{
    ui->PageTittle->setText(text);
    ui->musicImageLabel->setPixmap(QPixmap(imagePath));
    ui->musicImageLabel->setScaledContents(true);



}

void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    musicOfPage.clear();
    for(auto music :musicList){
        switch(musicListType){
        case LIKE_LIST:
            if(music.getIsLike()){

                musicOfPage.push_back(music.getMusicId());
            }
            break;
        case LOCAL_LIST:

            qDebug()<<"进来了2";
            qDebug()<<music.getMusicId();
            musicOfPage.push_back(music.getMusicId());
            break;
        case HISTORY_LIST:
            if(music.getIsHistory()){
                musicOfPage.push_back(music.getMusicId());
            }
            break;
        case NET_LIST:

            if(music.getIsFromNet()){

                musicOfPage.push_back(music.getMusicId());
            }
            break;
        default:
            qDebug()<<"暂不支持";
        }
    }

}

void CommonPage::reFresh(MusicList &musicList)
{
    ui->pageMusicList->clear();

    addMusicToMusicPage(musicList);
    for(auto musicId:musicOfPage){
        auto it=musicList.findMusicByMusicId(musicId);
        if(it==musicList.end())continue;

        ListItemBox*listItemBox=new ListItemBox(this);

        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setMusicSinger(it->getMusicSinger());
        listItemBox->setMusicAlbum(it->getMusicAlbumn());
        listItemBox->setIsLike(it->getIsLike());

        QListWidgetItem *item=new QListWidgetItem(ui->pageMusicList);
        item->setSizeHint(QSize(listItemBox->width(),listItemBox->height()));
        ui->pageMusicList->setItemWidget(item,listItemBox);

        connect(listItemBox,&ListItemBox::setLike,this,[=](bool isLike){
            it->setIsLike(isLike);

            emit updateLikeMusic(isLike,it->getMusicId());
        });

    }
    repaint();
}

void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playList)
{
    for(auto music:musicList){
        switch(musicListType){
        case LIKE_LIST:
            if(music.getIsLike()){
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_LIST:
            qDebug()<<"歌曲Url是:"<<music.getMusicUrl();
            playList->addMedia(music.getMusicUrl());
            break;
        case HISTORY_LIST:
            if(music.getIsHistory()){
                playList->addMedia(music.getMusicUrl());
            }
            break;
        case NET_LIST:
            if(music.getIsFromNet()){
                qDebug()<<"歌曲Url是:"<<music.getMusicUrl();
                playList->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug()<<"不支持此页面";

        }
    }
}

QString CommonPage::getMusicIdByIndex(int index)
{
    if(index>=musicOfPage.size()){
        qDebug()<<"越界";
    }
    return musicOfPage[index];
}

void CommonPage::setMusicImage(QPixmap pixmap)
{
    ui->musicImageLabel->setPixmap(pixmap);
    ui->musicImageLabel->setScaledContents(true);

}

void CommonPage::setCommonUi_Net(const QString &text, const QPixmap &imagePath)
{
    ui->PageTittle->setText(text);
    ui->musicImageLabel->setPixmap(imagePath);
    ui->musicImageLabel->setScaledContents(true);

}

MusicListType CommonPage::getPageListType()
{
    return this->musicListType;
}
